package com.neusoft.crm.controller;

import com.neusoft.crm.po.ClientInfo;
import com.neusoft.crm.po.ClientReprieveLose;
import com.neusoft.crm.service.IClientReprieveLoseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 暂缓流失客户信息前端接口类
 *
 * @author lanan1207
 */
@RestController
public class ClientReprieveLoseController {
    @Autowired
    private IClientReprieveLoseService lostClientService;

    /**
     * 显示暂缓流失客户列表
     *
     * @param clientInfo 请求体：客户信息
     * @return 客户信息列表
     */
    @RequestMapping("/selectDeferredLossCustomerList")
    public List<ClientInfo> selectDeferredLossCustomerList(@RequestBody ClientInfo clientInfo) {
        return lostClientService.selectDeferredLossCustomerList(clientInfo);
    }

    /**
     * 更新客户对应的暂缓流失项
     *
     * @param clientInfo 请求体：带有暂缓流失项列表的客户
     * @return 受影响的行数
     */
    @RequestMapping("/updateDeferredLossItem")
    public int updateDeferredLossItem(@RequestBody ClientInfo clientInfo) {
        // 获取现有暂缓流失项列表
        List<ClientReprieveLose> alreadyExistClientReprieveLose = lostClientService.selectDeferredLossItemById(clientInfo);
        // 判断已有的项是否有更新
        int i = 0, influenceLineCount = 0;
        List<ClientReprieveLose> updateRequestClientReprieveLoseList = new ArrayList<>();
        for (; i < alreadyExistClientReprieveLose.size(); i++) {
            if (!Objects.equals(clientInfo.getClientReprieveLoseList().get(i).getReprieveStep(), alreadyExistClientReprieveLose.get(i).getReprieveStep())) {
                // 把要更新的项存入到列表中统一更新，提高效率
                updateRequestClientReprieveLoseList.add(clientInfo.getClientReprieveLoseList().get(i));
                influenceLineCount++;
            }
        }
        // 判断更新的行数，对需要更新的记录进行更新
        if (updateRequestClientReprieveLoseList.size() != 0) {
            influenceLineCount += lostClientService.updateDeferredLossItemList(updateRequestClientReprieveLoseList);
            influenceLineCount--;
        }

        // 判断是否有新增，将if语句外置，降低CPU流水线运行开销，提高运行效率
        if (clientInfo.getClientReprieveLoseList().size() != alreadyExistClientReprieveLose.size()) {
            if (!Objects.equals(clientInfo.getClientReprieveLoseList().get(i).getReprieveStep(), "") && clientInfo.getClientReprieveLoseList().get(i).getReprieveStep() != null) {
                influenceLineCount += lostClientService.insertDeferredLossItem(clientInfo.getClientReprieveLoseList().get(i));
            }
        }
        return influenceLineCount;
    }

    /**
     * 更新暂缓流失项列表
     *
     * @param clientReprieveLoseList 需要更新的暂缓流失项列表
     * @return 受影响的行数
     */
    @RequestMapping("/updateDeferredLossItemList")
    public int updateDeferredLossItemList(@RequestBody List<ClientReprieveLose> clientReprieveLoseList) {
        return lostClientService.updateDeferredLossItemList(clientReprieveLoseList);
    }


    /**
     * 显示暂缓流失客户详情
     *
     * @param clientInfo 请求体：客户信息
     * @return 对应的流失客户详情
     */
    @RequestMapping("/selectLostCustomerDetails")
    public List<ClientInfo> selectLostCustomerDetails(@RequestBody ClientInfo clientInfo) {
        return lostClientService.selectLostCustomerDetails(clientInfo);
    }

    /**
     * 确认客户流失
     *
     * @param clientInfo 请求体：客户暂缓流失信息
     * @return 受影响的行数
     */
    @RequestMapping("/confirmLostClient")
    public int confirmLostClient(@RequestBody ClientInfo clientInfo) {
        return lostClientService.confirmLostClient(clientInfo);
    }

    /**
     * 挽救成功
     *
     * @param clientInfo 请求体：客户信息
     * @return 受影响的行数
     */
    @RequestMapping("/regainLostClient")
    public int regainLostClient(@RequestBody ClientInfo clientInfo) {
        return lostClientService.regainLostClient(clientInfo);
    }

    /**
     * 查询暂缓流失客户信息
     *
     * @param clientInfo 暂缓流失客户信息
     * @return 暂缓流失客户详情
     */
    @RequestMapping("/selectLostClient")
    public List<ClientInfo> selectLostClient(@RequestBody ClientInfo clientInfo) {
        return lostClientService.selectLostClient(clientInfo);
    }
}
